package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;

/**
 *
 * 下达监控指令
 * Created by lilongfei on 2018/7/24.
 */
public class JT_9101 implements JT808MsgBody{

    //服务器ip长度
    byte serverIpLength;
    //服务器ip
    String serverIp;
    //服务器tcp端口号
    short serverTcpPort;
    //服务器udp端口号
    short serverUdpPort;
    //监控逻辑通道号
    byte channel;
    //音视频数据类型 1 视频,2 双向对讲,3 监听,4 中心广播,5 透传
    byte type;
    //码流类型 0 主码流 ,1 子码流
    byte stream;

    public JT_9101(int channel,int stream,int type,short s_tcp,short s_ucp,String ip){
        this.setChannel(channel);
        this.setStream(stream);
        this.setType(type);
        this.setServerTcpPort(s_tcp);
        this.setServerUdpPort(s_ucp);
        this.setServerIp(ip);
        this.setIpLength(ip.length());
    }

    public byte getServerIpLength() {
        return serverIpLength;
    }

    public void setServerIpLength(byte serverIpLength) {
        this.serverIpLength = serverIpLength;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public short getServerTcpPort() {
        return serverTcpPort;
    }

    public void setServerTcpPort(short serverTcpPort) {
        this.serverTcpPort = serverTcpPort;
    }

    public short getServerUdpPort() {
        return serverUdpPort;
    }

    public void setServerUdpPort(short serverUdpPort) {
        this.serverUdpPort = serverUdpPort;
    }

    public byte getChannel() {
        return channel;
    }

    public void setChannel(byte channel) {
        this.channel = channel;
    }

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getStream() {
        return stream;
    }

    public void setStream(byte stream) {
        this.stream = stream;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buffer = new MyBuffer();
        buffer.put(getServerIpLength());
        buffer.put(getServerIp());
        buffer.put(getServerTcpPort());
        buffer.put(getServerUdpPort());
        buffer.put(getChannel());
        buffer.put(getType());
        buffer.put(getStream());
        return buffer.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        MyBuffer buffer = new MyBuffer(body);
        setServerIpLength(buffer.get());
        int lenght = BitConverter.ToUInt32(getServerIpLength());
        setServerIp(buffer.getString(lenght));
        setServerTcpPort(buffer.getShort());
        setServerUdpPort(buffer.getShort());
        setChannel(buffer.get());
        setType(buffer.get());
        setStream(buffer.get());
    }

    /**
     * 转换字段ip长度为byte类型,保存在消息体内
     * @param lenght
     */
    public void setIpLength(int lenght){
        byte[] b_lenght = BitConverter.GetBytes(lenght);
        //此处取低8位数据进行传输
        setServerIpLength(b_lenght[3]);
    }

    /**
     * 转换数字型通道号为byte对象
     * @param channel
     */
    public void setChannel(int channel){
        byte[] b_lenght = BitConverter.GetBytes(channel);
        //此处取低8位数据进行传输
        setChannel(b_lenght[3]);
    }

    /**
     * 转换数字型类型为byte对象
     * @param type
     */
    public void setType(int type){
        byte[] b_lenght = BitConverter.GetBytes(type);
        //此处取低8位数据进行传输
        setType(b_lenght[3]);
    }

    /**
     * 转换数字型码流为byte对象
     * @param stream
     */
    public void setStream(int stream){
        byte[] b_lenght = BitConverter.GetBytes(stream);
        //此处取低8位数据进行传输
        setStream(b_lenght[3]);
    }
}
